﻿using MES.Server.Entities;
using MES.Server.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES.Server.Service
{
    public class MaterialSerivce :BaseService, IMaterialService
    {
        public MaterialSerivce(ISqlSugarClient client) : base(client)
        {
        }

        public bool CheckClassName(string name, int id)
        {
            return _Client.Queryable<MaterialClassEntity>()
                .Any(d => d.ItemTypeName == name &&
                        d.ItemTypeId != id);
        }

        public int DeleteClass(int[] ids)
        {
            return _Client.Deleteable<MaterialClassEntity>()
                .In(ids).ExecuteCommand();
        }

        public IEnumerable<MaterialClassEntity> GetClassAll(string key)
        {
            var mcs = _Client.Queryable<MaterialClassEntity>()
                .Where(m =>
                string.IsNullOrEmpty(key) || m.ItemTypeName.Contains(key)
                )
                .ToList();

            return mcs;
        }

        public int UpdateClass(MaterialClassEntity entity)
        {
            int count;
            if (entity.ItemTypeId == 0)
            {
                // 新增
                count = _Client.Insertable(entity).ExecuteCommand();
            }
            else
            {
                // 更新
                count = _Client.Updateable(entity).ExecuteCommand();
            }

            return count;
        }
    }
}
